package query.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import query.controller.ManagerInterceptor;
import query.controller.UserInterceptor;

/**
 * 项目配置类，代替xml
 * 
 * @author lzy
 * @version 1.0.4
 * 
 */
@Configuration
@EnableWebMvc
@ComponentScan({ "query.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {

	/**
	 * 视图解析器
	 * 
	 * @return 视图解析器
	 */
	@Bean
	public ViewResolver initViewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}

	/**
	 * ,静态资源映射
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/icofont/**").addResourceLocations("/icofont/");
		registry.addResourceHandler("/images/**").addResourceLocations("/images/");
		registry.addResourceHandler("/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/user/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/user/icofont/**").addResourceLocations("/icofont/");
		registry.addResourceHandler("/user/images/**").addResourceLocations("/images/");
		registry.addResourceHandler("/user/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/user/questions/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/user/questions/icofont/**").addResourceLocations("/icofont/");
		registry.addResourceHandler("/user/questions/images/**").addResourceLocations("/images/");
		registry.addResourceHandler("/user/questions/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/paper/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/paper/icofont/**").addResourceLocations("/icofont/");
		registry.addResourceHandler("/paper/images/**").addResourceLocations("/images/");
		registry.addResourceHandler("/paper/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/paper/result/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/paper/result/icofont/**").addResourceLocations("/icofont/");
		registry.addResourceHandler("/paper/result/images/**").addResourceLocations("/images/");
		registry.addResourceHandler("/paper/result/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/paper/detail/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/paper/detail/icofont/**").addResourceLocations("/icofont/");
		registry.addResourceHandler("/paper/detail/images/**").addResourceLocations("/images/");
		registry.addResourceHandler("/paper/detail/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/paper/gotoPage/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/paper/gotoPage/icofont/**").addResourceLocations("/icofont/");
		registry.addResourceHandler("/paper/gotoPage/images/**").addResourceLocations("/images/");
		registry.addResourceHandler("/paper/gotoPage/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/paper/details/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/paper/details/icofont/**").addResourceLocations("/icofont/");
		registry.addResourceHandler("/paper/details/images/**").addResourceLocations("/images/");
		registry.addResourceHandler("/paper/details/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/manager/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/manager/icofont/**").addResourceLocations("/icofont/");
		registry.addResourceHandler("/manager/images/**").addResourceLocations("/images/");
		registry.addResourceHandler("/manager/js/**").addResourceLocations("/js/");
		super.addResourceHandlers(registry);
	}

	/**
	 * 拦截器添加类
	 */
	@Override
	public void addInterceptors(InterceptorRegistry arg0) {
		ManagerInterceptor interceptor = new ManagerInterceptor();
		UserInterceptor userInterceptor = new UserInterceptor();
		arg0.addInterceptor(interceptor).addPathPatterns("/manager/**");
		arg0.addInterceptor(userInterceptor).addPathPatterns("/user/**");
		arg0.addInterceptor(userInterceptor).addPathPatterns("/paper/**");
	}
}
